// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
import Loading from './Loading';
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    mapNode: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:
    private load: Loading;
    private loadObj: any;
    private loadCur = 0;
    private loadLen = 0;
    onLoad() {
        this.loadCur = 0;
        this.loadLen = 0;
        this.loadObj = {
            "Prefab": ["polie","ygou"],
            "Music": ["bgm2.mp3"]
        }
        this.load = new Loading();
        this.load.onLoad();
        this.load.Init();
        this.load.onBJ();
        this.PrefabObj = {};

        let _this = this;
        this.startLoad();
        let mapJs = this.mapNode.getComponent("Map");
        this.loadLen += mapJs.LoadLength();
        mapJs.Loading = function (c, l) {
            _this.onLoadTxt();
        }
        window["pool"] = {};
        window["pool"]["Prefab"] = {}
        window["pool"]["getPrefab"] = function (s) {
            if (window["pool"]["Prefab"][s]) {
                let arr: Array<cc.Node> = window["pool"]["Prefab"][s];
                if (arr.length > 1) {
                    let n = arr[arr.length - 1];
                    arr.splice(arr.length - 1, 1);
                    return n;
                } else if (arr.length > 0) {
                    return cc.instantiate(arr[0]);
                }
            }
        }
        window["pool"]["addPrefab"] = function (s, n: cc.Node) {
            if (window["pool"]["Prefab"][s]) {
                let arr: Array<cc.Node> = window["pool"]["Prefab"][s];
                arr[arr.length] = n;
            }
        }
    }
    private PrefabObj: any;
    private startLoad() {
        let _this = this;
        if (this.loadObj != null) {
            for (let s in this.loadObj) {
                this.loadLen += this.loadObj[s].length;
                let arr = this.loadObj[s];
                if (s == "Prefab") {

                    for (let i = 0; i < arr.length; i++) {
                        cc.loader.loadRes('Prefab/' + arr[i], function (err, prefab) {
                            if (!err) {
                                //   _this.PrefabObj[s]=cc.instantiate(prefab);
                                window["pool"]["Prefab"][arr[i]] = [cc.instantiate(prefab)]
                                _this.onLoadTxt();
                            } else {
                                _this.onLoadTxt();

                            }
                        });
                    }

                } else {
                    if (s == 'Music' || s == 'Sound') {
                        for (let i = 0; i < arr.length; i++) {
                            cc.loader.load(cc.url.raw('resources/' + s + '/' + arr[i]), function (err, sou) {
                                if (!err) {
                                    console.log("加载完成了？？？？")
                                    if (_this.MusicURL != sou) {
                                        _this.MusicURL = sou;
                                        cc.audioEngine.play(_this.MusicURL, true, 0.5);
                                    }
                                } else {
                                    _this.onLoadTxt();

                                }
                            });
                        }
                    }
                }
            }
        }
    }
    private MusicURL = null;
    private onLoadTxt() {
        this.loadCur++;

        //   this.loadtxt.string='正在加载素材'+this.loadCur+"/"+this.loadLen;
        this.load.setTxt(this.loadCur, this.loadLen);//
        //  cc.log(this.loadCur)
        if (this.loadCur >= this.loadLen) {
            //   this.StartBo=true;
            this.load.killPar();


        }
    }


    //  update (dt) {
    //  //   this.load.onStart();
    //  }
}
